<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2017/11/9  19:27
 */
namespace app\api\logic;
use app\api\BaseModel;
use think\Db;

class Address extends  BaseModel{

	//检测参数是否已传
    protected $ParameterDetection  = [
		'address_edit' => ['name','mobile','province','city','county','street','address'],
		'address_setdefault' => ['id'],
		'address_del' => ['id']
    ];

	//收货地址列表
    public function address_list()
    {
        $userid= $this->user['id'];
        $sel = db('user_address')->where(["user_id" => $userid])->select();
        return $sel;
    }

    //添加收货地址(修改收货地址)
    public function address_edit()
    {
        $userid= $this->user['id'];
        $name = $this->requestData["name"];
        $mobile = $this->requestData["mobile"];
        $province = $this->requestData["province"];
        $city = $this->requestData["city"];
        $area = $this->requestData["county"];
        $street = $this->requestData["street"];
        $address = $this->requestData["address"];
        $data = array(
            'name'        => $name,
            'mobile'      => $mobile,
            'province'    => $province,
            'city'        => $city,
            'country'     => $area,
            'street'      => $street,
            'detail'      => $address,
            'user_id'     => $userid,
        );
        if (!array_key_exists('id',$this->requestData)) {
            $count = db('user_address')->where(["user_id" => $userid])->count();
            if ($count > 10) {
                return array('message' => '个人收货地址不能超过10条');
            }
            if($count == 0){
                $data['isdefault'] = 1;
            }
            if (!db('user_address')->insert($data)) return array('message' => '添加失败');
        } else {
            if (db('user_address')->where(['address_id' => intval($this->requestData['id'])])->update($data) === false) return array('message' => '更新失败');
        }
        return array();
    }

    //设置默认收货地址
    public function address_setdefault()
    {
        $userid= $this->user['id'];
        $addressid = intval($this->requestData["id"]);
        $sel = db('user_address')->where(["address_id" => $addressid])->find();
        if (!$sel) {
            return array('message' => "收货地址不存在");
        }
        Db::startTrans();
        try{

            if(db('user_address')->where(["user_id" => $userid])->update(['isdefault' => 0]) === false) throw new \Exception('更新失败');

            if(db('user_address')->where(["user_id" => $userid,"address_id" => $addressid])->update(['isdefault' => 1]) === false) throw new \Exception('更新失败');
            Db::commit();
        }catch (\Exception $e){
            Db::rollback();
            return array('message' => '设置失败');
        }
        return array();
    }

    //删除收货地址
    public function address_del()
    {
        $userid= $this->user['id'];
        $addressid = intval($this->requestData["id"]);
        $sel = db('user_address')->where(["address_id" => $addressid,"user_id" => $userid])->find();
        if (!$sel) {
            return array('message' => "收货地址不存在");
        }
        if (!db('user_address')->where(["user_id" => $userid,"address_id" => $addressid])->delete()) return array('message' => "删除失败");
        return array();
    }
}